package com.jacky.service.impl;
import com.jacky.Main;
import com.jacky.po.AbstractUser;
import com.jacky.po.Admin;
import com.jacky.service.FileService;
import com.jacky.service.IdentifyService;
import com.jacky.service.UserService;
import com.sun.org.slf4j.internal.Logger;
import com.sun.org.slf4j.internal.LoggerFactory;

import java.io.*;
import java.util.Scanner;

/**
 * @Description: 用户服务的接口实现类
 * @author: Jacky
 * @date: 2021.10.12
 */
public class UserServiceImpl implements UserService {

    @Override
    public boolean login(AbstractUser user){
        if (user!=null){
            boolean flagName = false;
            boolean flagPwd = false;
            boolean flagIdentity = false;
            FileService fileService = new FileServiceImpl();
            File file = new File(user.getIdentity()+".txt");
            String[] strs = fileService.fileReading(file);
            for(String str:strs){
                //username:Jacky&password:123456  username:admin&password:admin
                String[] splits =str.split("&");
                for(String split:splits){
                    //username:Jacky  password:123456
                    String[] names =split.split(":");
                    // username Jacky
                    if("username".equals(names[0])&&user.getName().equals(names[1])){
                        flagName = true;
                    }else if("password".equals(names[0])&&user.getPassword().equals(names[1])) {
                        flagPwd = true;
                    }else if("identity".equals(names[0])&&user.getIdentity().equals(names[1])) {
                        flagIdentity = true;
                        break;
                    }
                }
            }
            if(flagName && flagPwd && flagIdentity){
                return true;
            } else {
                return false;
            }
        }else {
            System.out.println("没能正确选择身份，将返回首页，请重新选择功能和身份");
        }
        return false;
    }

    @Override
    public boolean register(AbstractUser user) {
        IdentifyService identifyService = new IdentifyServiceImpl();
        FileService fileService = new FileServiceImpl();
        File file = new File(user.getIdentity()+".txt");
        return fileService.fileWriting(file,user);
    };
}